
#include "DialogListItem.h"
#include "DialogListWidget.h"

DialogListItem::DialogListItem()
{
	_listWidget = 0;
	_form = Form_None;
	_selected = false;
	_visible = false;
	_dirty = true;
	_index = -1;
}
DialogListItem::~DialogListItem()
{

}

DialogListWidget* DialogListItem::listWidget()
{
	return _listWidget;
}

void DialogListItem::setIcon(BIcon* icon)
{
	if (_icon != icon)
	{
		_icon = icon;
	}
}
BIcon* DialogListItem::icon() const
{
	return _icon;
}

void DialogListItem::setText(const BString& text)
{
	if (_text != text)
	{
		_text = text;
		post(Signal_Dirty);
	}
}
const BString& DialogListItem::text() const
{
	return _text;
}

void DialogListItem::setAlign(Align align)
{
	if (_align != align)
	{
		_align = align;
		_dirty = true;
	}
}
Align DialogListItem::align() const
{
	return _align;
}

void DialogListItem::setForm(Form form)
{
	if(_form != form)
	{
		_form = form;
		_dirty = true;
		post(Signal_Dirty);
	}
}
Form DialogListItem::form() const
{
	return _form;
}

void DialogListItem::setRect(const BRect& rect)
{
	if (_rect != rect)
	{
		_rect = rect;
		_dirty = true;
		post(Signal_Dirty);
	}
}
const BRect& DialogListItem::rect() const
{
	return _rect;
}

void DialogListItem::freshSize(const BFont* font)
{
	_imageSize = _listWidget->iconSize();
	if (font)
	{
		_textSize = font->textSize(_text);
		if (_imageSize.width() < 1)
			_imageSize.width() = _textSize.height();
		if (_imageSize.height() < 1)
			_imageSize.height() = _textSize.height();
	}
	adjustRect(0, 0, 0, 0);
}
void DialogListItem::adjustRect(int x, int y, int w, int h)
{
	_rect.set(x, y, w, h);
	BValue value;
	_listWidget->query("spacing", value);
	int spacing = value;
	_hintSize.reset();
	_textRect.reset();
	_imageRect.reset();
	_textArea.reset();

	Form realForm = _form;
	if (_form == Form_None)
	{
		if (_imageSize.valid() && _textSize.valid())
			realForm = Form_IconText;
		else
		{
			if (_imageSize.valid())
				realForm = Form_IconOnly;
			if (_textSize.valid())
				realForm = Form_TextOnly;
		}
	}
	if (realForm == Form_IconAlone)
	{
		if (_imageSize.empty())
			realForm = Form_TextOnly;
		else
			realForm = Form_IconOnly;
	}
	if (realForm == Form_TextAlone)
	{
		if (_textSize.empty())
			realForm = Form_IconOnly;
		else
			realForm = Form_TextOnly;
	}
	switch (realForm)
	{
	case Form_IconOnly:
	{
		_imageArea = _rect;
		_hintSize = _imageSize;
		BRect rect = _imageArea.align(_align, _imageSize);
		_imageRect = rect;
		return;
	}
	case Form_TextOnly:
	{
		_textArea = _rect;
		_hintSize = _textSize;
		BRect rect = _textArea.align(_align, _textSize);
		_textRect = rect;
		return;
	}
	case Form_IconText:
	{
		_hintSize = BSize(_imageSize.width() + spacing + _textSize.width(), bMax(_imageSize.height(), _textSize.height()));
		BRect rect = _rect.align(_align, _hintSize);

		_imageArea = rect;
		_imageArea.setWidth(_imageSize.width());
		_imageRect = _imageArea.align(_align, _imageSize);

		_textArea = rect;
		_textArea.left() = rect.right() - _textSize.width();
		_textRect = _textArea.align(_align, _textSize);

		return;
	}
	case Form_TextIcon:
	{
		_hintSize = BSize(_imageSize.width() + spacing + _textSize.width(), bMax(_imageSize.height(), _textSize.height()));
		BRect rect = _rect.align(_align, _hintSize);

		_textArea = rect;
		_textArea.setWidth(_textSize.width());
		_textRect = _textArea.align(_align, _textSize);

		_imageArea = rect;
		_imageArea.left() = rect.right() - _imageSize.width();
		_imageRect = _imageArea.align(_align, _imageSize);

		return;
	}
	case Form_TextUnderIcon:
	{
		_hintSize = BSize(bMax(_imageSize.width(), _textSize.width()), _imageSize.height() + spacing + _textSize.height());
		BRect rect = _rect.align(_align, _hintSize);

		_imageArea = rect;
		_imageArea.setHeight(_imageSize.height());
		_imageRect = _imageArea.align(_align, _imageSize);

		_textArea = rect;
		_textArea.top() = rect.top() + _imageSize.height() + spacing;
		_textRect = _textArea.align(_align, _textSize);

		return;
	}
	case Form_IconUnderText:
	{
		_hintSize = BSize(bMax(_imageSize.width(), _textSize.width()), _imageSize.height() + spacing + _textSize.height());
		BRect rect = _rect.align(_align, _hintSize);

		_textArea = rect;
		_textArea.setHeight(_textSize.height());
		_textRect = _textArea.align(_align, _textSize);

		_imageArea = rect;
		_imageArea.top() = rect.top() + _textSize.height() + spacing;
		_imageRect = _imageArea.align(_align, _imageSize);
	}
	}
}
